package com.medical.admin.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.InformDto;
import com.medical.admin.sys.service.InformService;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Date : 2024/4/28 下午3:46
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/inform")
public class InformController {

    @Resource
    private InformService informService;

    @ApiOperation(value = "添加公告")
    @PostMapping("/add")
    public ResponseEntity<String> addInform(@RequestBody InformDto informDto) {
        Integer res = informService.addInform(informDto);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.INFORM_ADD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.TITLE_ADD_SUCCESS);
    }

    @ApiOperation(value = "修改公告")
    @PutMapping("/mod")
    public ResponseEntity<String> modInform(@RequestBody InformDto informDto) {
        Integer res = informService.modInform(informDto);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.INFORM_MOD_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.INFORM_MOD_ERROR);
    }

    @ApiOperation(value = "根据id查询公告")
    @GetMapping("/find")
    public ResponseEntity<InformDto> findInformById(@RequestParam("informId") Long informId) {
        InformDto informDto = informService.selectById(informId);
        if (ObjectUtil.isNotEmpty(informId)) {
            return ResponseEntity.success(informDto);
        }
        return ResponseEntity.error(ResponseCode.INFORM_FIND_ERROR);
    }

    @ApiOperation(value = "根据id删除公告")
    @PutMapping("/del")
    public ResponseEntity<String> delInformById(@RequestParam("informId") Long informId) {
        Integer res = informService.delById(informId);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.INFORM_DEL_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.INFORM_DEL_ERROR);
    }

    @ApiOperation(value = "批量删除公告")
    @PutMapping("/delBatch")
    public ResponseEntity<String> delInformByIds(@RequestBody List<Long> informIds) {
        Integer res = informService.delBatch(informIds);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.INFORM_DEL_BATCH_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.INFORM_DEL_BATCH_ERROR);
    }

    @ApiOperation(value = "根据id切换公告状态")
    @PutMapping("modStatus")
    public ResponseEntity<String> modStatus(@RequestParam("informId")Long informId, @RequestParam("informStatus") Integer informStatus){
        Integer res = informService.modStatus(informId,informStatus);
        if (res > 0) {
            return ResponseEntity.success(ResponseCode.INFORM_MOD_STATUS_SUCCESS.getMsg());
        }
        return ResponseEntity.error(ResponseCode.INFORM_MOD_STATUS_ERROR);
    }

    @ApiOperation(value = "分页查询公告")
    @GetMapping("/list")
    public ResponseEntity<Page<InformDto>> listInform(String informName, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<InformDto> page = informService.list(informName,pageNum,pageSize);
        if (ObjectUtil.isNotEmpty(page)) {
            return ResponseEntity.success(page);
        }
        return ResponseEntity.error(ResponseCode.INFORM_FIND_PAGE_ERROR);
    }

}
